1187C - Vasya And Array - CodeForces Solution


constructive algorithms greedy implementation *1800

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <math.h>
#include <chrono>
#include <iomanip>
#include <unordered_map>
#include <queue>
using namespace std;
#define ll long long
const int N = 1e2;
const int inf = 1e9;
 
ll gcd(ll a,ll b){
	if(b == 0) return a;
	return gcd(b,a%b);
}

struct custom_hash {
	static uint64_t splitmix64(uint64_t x) {
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
 
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
};
 
bool sortbyCond(const pair<int, int>& a, const pair<int, int>& b){
    if (a.first != b.first) return (a.first < b.first);
    else return (a.second > b.second);
}

// ll mn[N];
// // vector<ll> v(N);
// int dfs(int p) {
// 	if(mn[p] != -1) return mn[p];
// 	if(g[p].size() == 0) return v[p];
// 	ll sum = 0;
// 	for(int i=0;i<(int)g[p].size();i++) {
// 		sum += dfs(g[p][i]);
// 	}
// 	return mn[p] = min(v[p], sum);
// }

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n, m;
	cin >> n >> m;
	int pr[n] = {};
	vector<pair<int, int>> v, w;
	for(int i=0;i<m;i++) {
		int t, l, r;
		cin >> t >> l >> r;
		if(t == 1) {
			v.push_back({l, r});
		}
		else {
			w.push_back({l, r});
		}
	}
	sort(v.begin(), v.end());
	int en = 1;
	for(int i=0;i<(int)v.size();i++) {
		if(pr[v[i].first - 1] == 0) {
			en = 1;
		}
		for(int j=v[i].first - 1;j<v[i].second;j++) {
			if(pr[j] == 0) {
				pr[j] = en;
				en++;
			}
		}
	}
	en = 1e5;
	if(pr[0] == 0) {
		pr[0] = en;
	}
	int cnt = 0;
	for(int i=1;i<n;i++) {
		if(pr[i] == 0) {
			if(cnt == 0) {
				pr[i-1] += 1e5;
				cnt++;
			}
			pr[i] = pr[i-1] - 1;
		}
		else {
			cnt = 0;
		}
	}
	int lr = w.size();
	int ck[lr] = {};
	bool ok = false;
	for(int i=1;i<n;i++) {
		if(pr[i] < pr[i-1]) {
			for(int j=0;j<(int)w.size();j++) {
				int p = w[j].first, p1 = w[j].second;
				// cout<<p<<" "<<p1<<" "<<(i)<<" "<<(i + 1)<<"\n";
				if(p <= i && p1 >= (i + 1)) {
					ck[j] = 1;
				}
			}
		}
	}
	for(int i=0;i<lr;i++) {
		if(ck[i] == 0) {
			ok = true;
			break;
		}
	}
	if(ok) cout<<"NO\n";
	else {
		cout<<"YES\n";
		for(int i=0;i<n;i++) {
			cout<<pr[i]<<" ";
		}
	}

}


Comments

Submit
0 Comments
More Questions

1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff
734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number